package com.dmai.capacity.platform.user.service.impl;

import com.dmai.capacity.platform.user.model.SysArea;
import com.dmai.capacity.platform.user.service.ISysAreaService;
import com.dmai.capacity.platform.util.PinYinUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


import com.dmai.capacity.platform.user.mapper.SysAreaDao;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class SysAreaServiceImpl implements ISysAreaService {

    @Autowired
    private SysAreaDao areaDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysArea sysArea) {
        sysArea.setCreateTime(new Date());
        sysArea.setUpdateTime(sysArea.getCreateTime());

        areaDao.save(sysArea);
        log.info("保存区域：{}", sysArea);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SysArea sysArea) {
        sysArea.setUpdateTime(new Date());

        areaDao.updateByOps(sysArea);
        log.info("修改区域：{}", sysArea);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        SysArea sysArea = areaDao.findById(id);

        areaDao.delete(id);

        log.info("删除区域：{}", sysArea);
    }


    @Override
    public SysArea findById(String id) {
        return areaDao.findById(id);
    }

    @Override
    @Cacheable(value = "area", unless = "#result == null")
    public String getFullName(String id) {
        SysArea self = areaDao.findById(id);
        if (self == null) {
            return "";
        }
        if (self.getParentId().equals("-1")) {
            return self.getAreaName();
        }
        return getFullName(self.getParentId()) + self.getAreaName();
    }

    @Override
    public List<SysArea> findAreas(Map<String, Object> params) {
//        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, CommonConstant.PAGE_CURRENT) != null && MapUtils.getInteger(params, CommonConstant.PAGE_SIZE) != null)
//            PageHelper.startPage(MapUtils.getInteger(params, CommonConstant.PAGE_CURRENT), MapUtils.getInteger(params, CommonConstant.PAGE_SIZE), true);
        List<SysArea> list = areaDao.findList(params);
        return list;
    }

    @Override
    public List<SysArea> findByParentId(String parentId) {
        return areaDao.findByParentId(parentId);
    }


    @Override
    public boolean saveOrUpdate(SysArea sysArea, String keyValue) {
        int i = 0;
        if (!sysArea.getParentId().equals("0")) {
            SysArea parent = areaDao.findById(sysArea.getParentId());
            if (parent != null) {
                sysArea.setLayer(parent.getLayer() + 1);
            }
        } else {
            sysArea.setLayer(1);
        }
        String simpleSpelling = PinYinUtil.getAlpha(sysArea.getAreaName());
        String quickQuery = PinYinUtil.getPingYin(sysArea.getAreaName());
        sysArea.setQuickQuery(quickQuery);
        sysArea.setSimpleSpelling(simpleSpelling);

        if (StringUtils.isNotEmpty(keyValue)) {
            SysArea model = areaDao.findById(keyValue);
            if (model != null) {
                sysArea.setUpdateTime(new Date());
                i = areaDao.updateByOps(sysArea);
            }
        } else {
            sysArea.setCreateTime(new Date());
            sysArea.setUpdateTime(sysArea.getCreateTime());
            i = areaDao.save(sysArea);
        }

        return i > 0;
    }

}
